<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>So, you want to start your meeting?</title>
    <link rel="stylesheet" type="text/css" href="styles/startMeeting.css" media="screen" />
	<link rel="stylesheet" type="text/css" href="styles/smoothness/jquery-ui-1.8.6.custom.css"  />
    <script type="text/javascript" src="scripts/jquery-1.4.2.min.js" ></script>
    <script type="text/javascript" src="scripts/underscore-min.js"></script>
	<script type="text/javascript" src="scripts/jquery-ui-1.8.5.custom.min.js"></script>
    <script type="text/javascript" src="scripts/jquery.layout.min-1.2.0.js"></script>
    <script type="text/javascript" src="scripts/shortcuts.js"></script>
    <script type="text/javascript" src="scripts/jquery.qtip-1.0.0-rc3.min.js"></script>
    <script type="text/javascript" src="scripts/wikid-0.2.js" ></script>
    <script type="text/javascript" src="scripts/date.js" ></script>
    <script type="text/javascript" src="scripts/couch.js"></script>
	<script type="text/javascript" src="scripts/jquery.couch.js"></script>
    <script type="text/javascript" src="scripts/meeting.js" ></script>
    <script type="text/javascript">

        var selectedAgenda = null;
        var selectedMeeting = null;
        $(function() {
            var db_name = wikid.parseDBname(window.location);
            var db = new CouchDB(db_name);
            $('#agenda').hide();
            $('#previous').hide();


            $("input[value='no']").click(function() {
                $('#previous').hide();
                $('#agenda').hide();
            });

            $("input[value='previous']").click(function() {
                $('#agenda').hide();
                $('#previous').show('clip', {}, 500);

            });

            $("input[value='agenda']").click(function() {
                $('#previous').hide();
                $('#agenda').show('clip', {}, 500);

            });

            $("#meetingName").change(function() {
                var topicName = getMeetingTopicId();
               $('.topicName').text(topicName);
            });

            $('button').button().click(function() {

                var topicId = getMeetingTopicId();
                $('.topicName').text(topicId);
                var now = new Date().getTime();

                var meeting = {
                    "_id": topicId,
                    "-tags":["Meeting"],
                    "-type" : "Conversation",
                    "-Recording" : {
                        "-startDate" : now
                    }
                };
                var agenda = getAgenda();
                if (agenda && _.isArray(agenda.items)  && agenda.items.length > 0) {
                    meeting['-agenda'] = agenda.items;
                    meeting['-tags'].push(agenda.id);
                }
                if (agenda && agenda.attendees) {
                    meeting['-attendees'] = agenda.attendees;
                }

                var recordScreen = $('#recordScreen').is(':checked');
                if (recordScreen) {
                    meeting["-Recording"].recordScreen = true;
                }

                var diarize = $('#diarize').is(':checked');
                if (diarize) {
                    meeting["-Recording"].diarize = true;
                }


                var existingMeeting = db.open(topicId);
                if (existingMeeting) {
                    $('.topicName').addClass('ui-state-error');
                }
                else {
                    $('.topicName').removeClass('ui-state-error');
                    db.save(meeting);
                    window.location = "outliner.html?" + topicId;
                }

            });

            // get the agendas
            var agendas = db.view('system/doc_by_tag',{key: 'Agenda'}).rows;
            if (agendas) {
                var agendaNames = _.pluck(agendas, 'id');
                var output = ['<option></option>'];
                $.each(agendaNames, function(key, val) {
                    output.push('<option value="'+ val +'">'+ wikid.titleFromTopicId(val) +'</option>')
                });
                $('#AgendaSelect')
                        .html(output.join(' '))
                        .bind('change click', function() {
                            var meeting = $(this).val();
                            if (!meeting){
                                selectedAgenda = null;
                                $("#meetingName").val('').change();
                            }
                            else {
                                selectedAgenda = meeting;
                                $("#meetingName").val(meeting).change();
                            }
                        });
            }
            // get previous meetings
            var meetings = db.view('system/doc_by_tag',{key: 'Meeting'}).rows;
            if (meetings) {
                var meetingNames = _.pluck(meetings, 'id');
                var output = ['<option></option>'];
                $.each(meetingNames, function(key, val) {
                    output.push('<option value="'+ val +'">'+ wikid.titleFromTopicId(val) +'</option>')
                });
                $('#previousMeeting')
                        .html(output.join(' '))
                        .bind('change click', function() {
                            var meeting = $(this).val();
                            if (!meeting) {
                                 selectedMeeting = null;
                                 $("#meetingName").val('').change();
                            } else {
                                selectedMeeting = meeting;
                                meeting = meeting.replace(/(_)?\(\d+-\w{1,3}-\d+\)/,'');

                                $("#meetingName").val(meeting).change();
                            }
                        });
            }

            function getAgenda() {
                if (selectedAgenda) {
                    var doc = db.open(selectedAgenda);
                    var agenda = {
                        id : doc._id
                    };


                    if (doc.Agenda && _.isArray(doc.Agenda.Attendees)) {
                        agenda.attendees =  doc.Agenda.Attendees;
                    }
                    agenda.items = meeting.parseAgenda(doc['-wiki']);

                    return agenda;
                }
            }


        });




        function getMeetingTopicId() {
            var day = Date.today().toString('d-MMM-yyyy');
            var title = $.trim($('#meetingName').val());
            if (!title) {
                return '';
            }

            return wikid.topicIdFromTitle(title + " (" + day + ")");
        }

    </script>
</head>
<body>
    <h2>So you wanna meet?</h2>



    <div class="fieldHeader">Do you have an agenda?</div>
    <div>
        <input type="radio" name="agenda" value="no" checked="checked"/> No, start from a blank slate.
    </div>
    <div>
        <input type="radio" name="agenda" value="previous" /> No, but create one now based on a previous meeting.
    </div>
    <div id="previous" class="indent">
        Meeting
        <select name="agenda" id="previousMeeting">
        </select>
    </div>
    <div>
        <input type="radio" name="agenda" value="agenda"/> Yes
    </div>
    <div id="agenda" class="indent">
        Agenda
        <select name="agenda" id="AgendaSelect">
        </select>
    </div>
    <div>
        <div class="fieldHeader">Meeting Name</div>
        <input id="meetingName" type="text" name="meetingName" id="meetingName"/>
        <div class="topicName"></div>
    </div>

    <div>
        <input type="checkbox" id="recordScreen" /> Record this computer screen.
    </div>
    <div>
        <input type="checkbox" id="diarize" /> Automatically mark interesting moments (warning, fast computer required.)
    </div>


    <button id="startButton">Start</button>

</body>
</html>